package com.itzz.spring_zhoneHeng.dao;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itzz.spring_zhoneHeng.pojo.Order;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Mapper
public interface OrderDao extends BaseMapper<Order> {

    //添加订单
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    @Insert("insert into `order`(id,book_name,author,path,date,state,money) " +
            "values(#{id},#{bookName},#{author},#{path},#{date},#{state},#{money})")
    int addOrder(Order order);

    //添加O_C表  (也就是订单书籍的类型表)
    @Insert("insert into o_c(oid,classid) " +
            "values(#{id},#{classId})")
    int addO_C(Integer id,Integer classId);

    //查询所有的订单
    List<Order> queryAllOrder(Order order);

    //根据id删除订单
    @Delete("delete from `order` where id=#{id}")
    int delOrder(Integer id);

    //根据id删除对应关系表O_C
    @Delete("delete from `o_c` where oid=#{id}")
    int delO_C(Integer id);

    //根据订单id修改订单的支付状态
    @Update("update `order` set state=#{state} where id=#{id}")
    int updateOrderState(Integer id,Integer state);


    //添加订单时添加o_b表   oid  bookid
    @Insert("insert into o_b(oid,bookid) values(#{id},#{bookId})")
    int addO_B(Integer id,Integer bookId);


    //根据bookId和userId查询order表中的state
    @Select("SELECT o.state from `order` o JOIN u_o uo on o.id=uo.orderid \n" +
            "JOIN o_b ob on o.id=ob.oid where ob.bookid=#{bookId} and uo.userid=#{userId}")
    Integer queryOrderState(@Param("bookId") Integer bookId,@Param("userId") Integer userId);

//    删除对应的O_B关系表   根据oid
    @Delete("delete from o_b where oid=#{id}")
    int delO_B(Integer id);


    //根据orderId  查询到对应的O_B表中的bookid
    @Select("select bookid from o_b where oid=#{orderId}")
    Integer queryBookId(Integer orderId);

    //根据orderId  查询order表中的state
    @Select("select state from `order` where id=#{orderId}")
    Integer queryState(Integer orderId);

    //根据userId 和orderid 添加表 u_o
    @Insert("insert into u_o(userid,orderid) values(#{userId},#{orderId})")
    Integer addU_O(Integer userId,Integer orderId);

    //根据orderid  删除表 u_o
    @Delete("delete from u_o where orderid=#{orderId}")
    Integer delU_O(Integer orderId);

}
